#include "theater.h"
#include "synch.h"
#include "system.h"


//the manager told the concessclerk to work
void concessionClerk_startWork(unsigned int conclerk_workno)
{
    DBGPRINT(DEBUG_TRACE,"concession clerk %d is going to work",conclerk_workno);
    while(1)
    {
check_off:
    concessClerkMsgLock[conclerk_workno]->Acquire();
        if(OFF_WORK==concessclrk_off[conclerk_workno])
        {
            DBGPRINT(DEBUG_INFO, "the manager tell me to break, concessClerkMsgLock %d wait here \n ", conclerk_workno);
			printf("ConcessionClerk [%d] is going on break.\n", conclerk_workno);
            if(concessclrk_cus_count[conclerk_workno]>0)
            {
                concessclerkline_cv[conclerk_workno]->Broadcast(concessClerkLineLock);
            }
            concessclrk_cus_count[conclerk_workno]=0;
            concessclrk_state[conclerk_workno]=0;
            concessclerkMsg_cv[conclerk_workno]->Wait(concessClerkMsgLock[conclerk_workno]);
            concessClerkMsgLock[conclerk_workno]->Release();
            DBGPRINT(DEBUG_INFO, "the manager tell me to work on, conclerk_workno %d start to work \n ", conclerk_workno);
			printf("ConcessionClerk [%d] is coming off break.\n", conclerk_workno);
        }
        concessClerkLineLock->Acquire();// to get the lock of the line
        if(0==concessclrk_cus_count[conclerk_workno]) //if no one is in the line
        {
            DBGPRINT(DEBUG_INFO,"conclerk_workno %d is free right now, have a coffee and wait for the counter\n", conclerk_workno);
			printf("ConcessionClerk [%d] has no one in line. I am available for a customer.\n", conclerk_workno);
            concessclrk_state[conclerk_workno]=0;
        }
        else //if some one
        {
            //step 1: dec the count of the customer line
            DBGPRINT(DEBUG_INFO,"conclerk_workno %d need to wake up the customer\n", conclerk_workno);
			printf("ConcessionClerk [%d] has a line length [%d] and is signaling a customer.\n", conclerk_workno, concessclrk_cus_count[conclerk_workno]);
            //step 2: wake up a customer from the list
            concessclerkline_cv[conclerk_workno]->Signal(concessClerkLineLock);
        }

        //step 1: dec the count of the customer line
        //step 2: wake up a customer from the list
        //step 3: wait for the customer to tell how many popcorn and soda they want to buy
        concessClerkLineLock->Release();
        concessClerkMsgLock[conclerk_workno]->Acquire();
        concessclerkMsg_cv[conclerk_workno]->Wait(concessClerkMsgLock[conclerk_workno]);
        //right here, we need to check i am waked up by the customer to buy or the manager to break
        if(OFF_WORK==concessclrk_off[conclerk_workno]||(popcorn_num[conclerk_workno]==0)&&(0==soda_num[conclerk_workno]))
        {
            DBGPRINT(DEBUG_INFO,"conclerk_workno %d are told to off work\n", conclerk_workno);
            concessClerkMsgLock[conclerk_workno]->Release();
            goto check_off;
        }
        concessClerkLineLock->Acquire();
        concessclrk_state[conclerk_workno]=1;
        concessclrk_cus_count[conclerk_workno]--;
         food_money[conclerk_workno]=POPCORN_PRICE * popcorn_num[conclerk_workno]+  SODA_PRICE * soda_num[conclerk_workno];
        popcorn_totalnum[conclerk_workno]+=popcorn_num[conclerk_workno];
        soda_totalnum[conclerk_workno]+=soda_num[conclerk_workno];
        food_totalmoney[conclerk_workno]+=food_money[conclerk_workno];
		printf("ConcessionClerk [%d] has an order for [%d] popcorn and [%d] soda. The cost is [%d].\n", conclerk_workno, popcorn_totalnum[conclerk_workno], soda_totalnum[conclerk_workno], food_totalmoney[conclerk_workno]);
        concessClerkLineLock->Release();
        popcorn_num[conclerk_workno]=0;
        soda_num[conclerk_workno]=0;
        DBGPRINT(DEBUG_INFO,"conclerk_workno %d say hi to the customer\n", conclerk_workno);
        //step 4:waked up by the customer and read how much they need to pay
       
        //wake up by the customer and receive the money
        concessclerkMsg_cv[conclerk_workno]->Signal(concessClerkMsgLock[conclerk_workno]);
        concessClerkMsgLock[conclerk_workno]->Release();
		printf("ConcessionClerk [%d] has been paid for the order.\n", conclerk_workno);
		sendMoneyToManagerFromConcessionClerk(conclerk_workno, food_money[conclerk_workno]);
    }
};


